package com.itheima.hmshopping.dubbo.apiImpl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.hmshopping.dubbo.api.PmsBrandApi;
import com.itheima.hmshopping.entity.PmsBrand;
import com.itheima.hmshopping.dubbo.mapper.PmsBrandMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class PmsBrandApiImpl implements PmsBrandApi {

    @Autowired
    private PmsBrandMapper pmsBrandMapper;

    /**
     * 获取全部品牌列表
     *
     * @return
     */
    @Override
    public List<PmsBrand> getListAll() {
        LambdaQueryWrapper<PmsBrand> queryWrapper = new LambdaQueryWrapper<>();
        return pmsBrandMapper.selectList(queryWrapper);
    }


    /**
     * 根据品牌名称分页获取品牌列表
     *
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @return
     */
    @Override
    public Page findByBrand(Integer pageNum, Integer pageSize, String keyword) {
        Page<PmsBrand> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<PmsBrand> queryWrapper = new LambdaQueryWrapper<>();
        //1->模糊查询品牌
        queryWrapper.like(StrUtil.isNotBlank(keyword), PmsBrand::getName, keyword);
        Page<PmsBrand> pmsBrandPage = pmsBrandMapper.selectPage(page, queryWrapper);
        return pmsBrandPage;
    }


    /**
     * 根据编号查询品牌信息
     *
     * @param id
     * @return
     */
    @Override
    public PmsBrand findById(Integer id) {
        LambdaQueryWrapper<PmsBrand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PmsBrand::getId, id);
        return pmsBrandMapper.selectById(id);

    }


    /**
     * 添加品牌
     *
     * @param brand
     */
    @Override
    public void save(PmsBrand brand) {
        pmsBrandMapper.insert(brand);
    }


    /**
     * 更新品牌
     *
     * @param id
     * @param pmsBrand
     */
    @Override
    public void updateById(Integer id, PmsBrand pmsBrand) {
        LambdaQueryWrapper<PmsBrand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(PmsBrand::getId, id);
        pmsBrandMapper.updateById(pmsBrand);
    }


    /**
     * 删除品牌
     *
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        pmsBrandMapper.deleteById(id);
    }


    /**
     * 批量删除品牌
     *
     * @param ids
     */
    @Override
    public void deleteByIds(Integer[] ids) {
        LambdaQueryWrapper<PmsBrand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(PmsBrand::getId, ids);
        pmsBrandMapper.delete(queryWrapper);
    }


    /**
     * 批量更新显示状态
     *
     * @param ids
     * @param showStatus
     */
    @Override
    public void updateStatus(Integer[] ids, Integer showStatus) {
        LambdaQueryWrapper<PmsBrand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(PmsBrand::getId, ids);
        PmsBrand pmsBrand = new PmsBrand();
        pmsBrand.setShowStatus(0);
        pmsBrandMapper.update(pmsBrand, queryWrapper);
    }

    @Override
    public void updateFactoryStatus(Integer[] ids, Integer factoryStatus) {
        LambdaQueryWrapper<PmsBrand> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(PmsBrand::getId, ids);
        PmsBrand pmsBrand = new PmsBrand();
        pmsBrand.setFactoryStatus(0);
        pmsBrandMapper.update(pmsBrand, queryWrapper);
    }
}